package com.iam.api.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.iam.api.service.SysUserService;
import com.iam.core.base.BaseCommonController;
import com.iam.core.base.BaseCommonService;
import com.iam.core.entities.Result;
import com.iam.core.model.SysUser;
import com.iam.core.utils.ResultUtil;
import com.iam.core.model.SysFile;
import com.iam.core.service.SysFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

@RestController
@RequestMapping("/api/sysFile")
public class SysFileController extends BaseCommonController<SysFile> {

    @Autowired
    private SysFileService<SysFile> service;

    @Autowired
    private SysUserService<SysUser> userService;

    @PostMapping("/import")
    public Result importInfo(@RequestParam("file") MultipartFile file) throws NoSuchAlgorithmException, IOException {
        SysUser currentUser = userService.getById(StpUtil.getLoginIdAsInt());
        SysFile sysFile = new SysFile();
        sysFile.setFileName(file.getOriginalFilename());
        sysFile.setFileCode(generateUniqueCode());
        sysFile.setFilePath("c://uploadPath/" + sysFile.getFileCode());
        uploadFile(file, "c://uploadPath/", sysFile.getFileCode());
        service.save(sysFile);
        return ResultUtil.success(sysFile);
    }


    @Override
    public BaseCommonService<SysFile> getiService() {
        return service;
    }

    @Override
    public String getMouduleName() {
        return "文件";
    }

    public static String generateUniqueCode() throws NoSuchAlgorithmException {
        long timestamp = System.currentTimeMillis();
        Random random = new Random();
        int randomNumber = random.nextInt(900000) + 100000;
        String input = timestamp + String.valueOf(randomNumber);

        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(input.getBytes());
            byte[] digest = md.digest();
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(String.format("%02x", b & 0xff));
            }
            return sb.toString().substring(0, 10);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void uploadFile(MultipartFile file, String uploadDir, String newFileName) throws IOException {
        // 创建目标目录（如果不存在）
        File dir = new File(uploadDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        // 构建完整路径并保存文件
        File dest = new File(uploadDir + File.separator + newFileName);
        file.transferTo(dest);
    }

}
